﻿using DevExpress.XtraGrid.Views.Grid;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Resources;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Layout;
using DevExpress.XtraGrid.Views.Card;
using DevExpress.XtraRichEdit;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.EditForm.Helpers.Controls;

namespace NAGFwk.UI
{
    public partial class BaseCustomEditForm : EditFormUserControl
    {
        private bool _IsNew = false;
        private bool _IsEdit = false;
        private bool _IsOpen = false;

        public bool IsNew
        {
            get { return _IsNew; }
            set
            {
                _IsNew = value;
                _IsEdit = !value;
                _IsOpen = !value;
            }
        }
        public bool IsEdit
        {
            get { return _IsEdit; }
            set
            {
                _IsEdit = value;
                _IsNew = !value;
                _IsOpen = !value;
            }
        }
        public bool IsOpen
        {
            get { return _IsOpen; }
            set
            {
                _IsEdit = !value;
                _IsNew = !value;
                _IsOpen = value;
            }
        }

        public BaseCustomEditForm()
        {
            InitializeComponent();

        }

        private void TraverseControls(Control c, bool IsReadOnly)
        {
            CheckEditors(c, IsReadOnly);
            CheckRichEditControl(c, IsReadOnly);
            CheckGridControl(c, IsReadOnly);
            CheckOther(c, IsReadOnly);
        }
        private void CheckOther(Control c, bool IsReadOnly)
        {
            foreach (Control control in c.Controls)
                TraverseControls(control, IsReadOnly);
        }
        private void CheckEditors(Control c, bool IsReadOnly)
        {
            if (c is BaseEdit)
                ((BaseEdit)c).Properties.ReadOnly = IsReadOnly;
        }
        private void CheckRichEditControl(Control c, bool IsReadOnly)
        {
            if (c is RichEditControl)
                ((RichEditControl)c).ReadOnly = IsReadOnly;
        }
        private void CheckGridControl(Control c, bool IsReadOnly)
        {
            if (c is GridControl)
            {
                GridControl gridControl = (GridControl)c;

                for (int j = 0; j < gridControl.ViewCollection.Count; j++)
                {
                    if (gridControl.ViewCollection[j] is GridView)                    
                        (gridControl.ViewCollection[j] as GridView).OptionsBehavior.Editable = !IsReadOnly;                                            

                    if (gridControl.ViewCollection[j] is LayoutView)                    
                        (gridControl.ViewCollection[j] as LayoutView).OptionsBehavior.Editable = !IsReadOnly;                                            

                    if (gridControl.ViewCollection[j] is CardView)
                        (gridControl.ViewCollection[j] as CardView).OptionsBehavior.Editable = !IsReadOnly;                    

                    if (gridControl.ViewCollection[j] is BandedGridView)
                        (gridControl.ViewCollection[j] as BandedGridView).OptionsBehavior.Editable = !IsReadOnly;                    
                }
            }
        }

        private void BaseCustomEditForm_Enter(object sender, System.EventArgs e)
        {
            if (IsOpen)
                TraverseControls(this, true);
            else
                TraverseControls(this, false);
        }

        private void SetButtonsAction()
        {
            foreach (Control control in this.Controls)
            {
                if (!(control is EditFormContainer))
                    continue;

                foreach (Control nestedControl in control.Controls)
                {
                    if (!(nestedControl is PanelControl))
                        continue;

                    foreach (Control button in nestedControl.Controls)
                    {
                        if (!(button is SimpleButton))
                            continue;

                        var simpleButton = button as SimpleButton;
                        if (IsOpen)
                            simpleButton.Text = "Salir";

                        break;
                    }
                }
            }
        }
    }
}
